﻿using System;
using System.Globalization;
using System.Xml.Serialization;

namespace FlickrSL.Data
{
    [XmlType("dates")]
    public class PhotoDates
    {
        [XmlIgnore]
        public DateTime? DatePosted { get; set; }

        [XmlAttribute("posted")]
        public String DatePostedString
        {
            get
            {
                if (this.DatePosted.HasValue)
                {
                    return ((Int64)this.DatePosted.Value.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds).ToString();
                }
                else
                {
                    return null;
                }
            }

            set
            {
                if (!String.IsNullOrEmpty(value))
                {
                    this.DatePosted = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(Int64.Parse(value));
                }
                else
                {
                    this.DatePosted = null;
                }
            }
        }

        [XmlIgnore]
        public DateTime? DateTaken { get; set; }

        [XmlAttribute("taken")]
        public String DateTakenString
        {
            get
            {
                if (this.DateTaken.HasValue)
                {
                    return this.DateTaken.Value.ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    return null;
                }
            }

            set
            {
                if (!String.IsNullOrEmpty(value))
                {
                    this.DateTaken = DateTime.ParseExact(value, "yyyy-MM-dd HH:mm:ss", CultureInfo.CurrentCulture);
                }
                else
                {
                    this.DateTaken = null;
                }
            }
        }

        [XmlIgnore]
        public DateGranularity DateTakenGranularity { get; set; }

        [XmlAttribute("takengranularity")]
        public Int32 DateTakenGranularityValue
        {
            get { return (Int32)this.DateTakenGranularity; }
            set { this.DateTakenGranularity = (DateGranularity)value; }
        }

        [XmlIgnore]
        public DateTime? LastUpdate { get; set; }

        [XmlAttribute("lastupdate")]
        public String LastUpdateString
        {
            get
            {
                if (this.LastUpdate.HasValue)
                {
                    return ((Int64)this.LastUpdate.Value.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds).ToString();
                }
                else
                {
                    return null;
                }
            }

            set
            {
                if (!String.IsNullOrEmpty(value))
                {
                    this.LastUpdate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(Int64.Parse(value));
                }
                else
                {
                    this.LastUpdate = null;
                }
            }
        }
    }
}
